﻿/*
 * Created by SharpDevelop.
 * User: 김효순
 * Date: 2012-08-30
 * Time: 오후 2:10
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 바탕화면
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private const int SPI_SETDESKWALLPAPER = 0x14;
		private const int SPIF_UPDATEINIFILE = 0x1;
		private const int SPIF_SENDCHANGE = 0x2;
		
		private const int VK_LWIN = 0x5b;
		
		private const int KEYEVENTF_KEYUP = 0x2;

		
		[DllImport("user32.dll")]
		private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
		
		[DllImport("user32.dll")]
		private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
		
		[DllImport("user32.dll")]
		private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
		
		[DllImport("user32.dll")]
		private static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
		
		[DllImport("user32.dll")]
		private static extern IntPtr GetParent(IntPtr hWnd);
		
		[DllImport("user32.dll")]
		private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
		
		private IntPtr sysListView;
		private IntPtr oParent;


		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Bt1Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "이미지파일|*.jpg;*.bmp;*.gif;*.png|All Files(*.*)|*.*";
			
			try{
				if(ofd.ShowDialog() == DialogResult.OK){
					SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, ofd.FileName , SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
				}
			} catch(System.Exception ex){
				MessageBox.Show("File Open Error: " + ex.Message);
			}
		}
		
		void Bt2Click(object sender, EventArgs e)
		{
			char VK_D = 'D';
			
			keybd_event(VK_LWIN, 0, 0, 0);
			keybd_event((byte)VK_D, 0, 0, 0);
			keybd_event((byte)VK_D, 0, KEYEVENTF_KEYUP, 0);
			keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
		}
		
		// 작동안됨
		void Bt3Click(object sender, EventArgs e)
		{
			IntPtr Progman = FindWindow("Progman", null);
			IntPtr SHELLDLLDefView = FindWindowEx(Progman, IntPtr.Zero , "SHELLDLL_DefView", null);
			MessageBox.Show(SHELLDLLDefView.ToString());
			this.sysListView = FindWindowEx(SHELLDLLDefView, (IntPtr)0, "SysListView32", null);
			this.oParent = GetParent(this.sysListView);
			SetParent(this.sysListView, this.Handle);
		}
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			if(this.sysListView.Equals(IntPtr.Zero) || this.oParent.Equals(IntPtr.Zero)) return;
			SetParent(this.sysListView, this.oParent);
		}
	}
}
